﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Panel_LuckyDraw : BasePanel
{
    CanvasGroup canvas;
    public GameObject pointer;
    public Button button;

    private bool IsRotating;
    private float spd;

    private void Awake()
    {
        canvas = transform.GetComponent<CanvasGroup>();
    }

    public override void OnEnter()
    {
        base.OnEnter();
        gameObject.SetActive(true);
    }

    public override void OnExit()
    {
        base.OnExit();
        IsRotating = false;
        pointer.transform.rotation = Quaternion.identity;
        gameObject.SetActive(false);

    }

    public override void OnPause()
    {
        base.OnPause();
        canvas.alpha = 0;
        canvas.blocksRaycasts = false;
    }

    public override void OnResume()
    {
        base.OnResume();
        canvas.alpha = 1;
        canvas.blocksRaycasts = true;
    }

    public void ClosePanel()
    {
        UIManager.Instance.PopPanel();
    }

    private void FixedUpdate()
    {
        if (IsRotating)
        {
            pointer.transform.Rotate(0, 0, spd * Time.deltaTime);
        }

        if (Input.GetButtonDown("Cancel"))
        {
            GameControl.instance.GameOver();
            UIManager.Instance.PopPanel();
        }
    }


    /// <summary>
    /// 抽奖按钮
    /// </summary>
    public void Button_Click()
    {
        User.instance.luckyBool = true;
        GoogleAdMobController.instance.ShowRewardAd();
    }

    public void LuckyDrawRotate()
    {
        StartCoroutine(PointerRotate());
    }

    /// <summary>
    /// 转盘转动
    /// </summary>
    /// <returns></returns>
    IEnumerator PointerRotate()
    {
        spd = Random.Range(8, 15) * -100;
        IsRotating = true;
        button.GetComponent<Button>().enabled = false;      //转动按键不可用
        yield return new WaitForSeconds(2);
        IsRotating = false;
        button.GetComponent<Button>().enabled = true;
        OnPointerStop();
    }

    /// <summary>
    /// 转盘停止,获取结果
    /// </summary>
    private void OnPointerStop()
    {
        float angle = Vector2.Angle(Vector2.up, pointer.transform.up);
        if (Vector2.Angle(Vector2.right, pointer.transform.up) > 90)
        {
            angle = -angle;
        }

        if (Mathf.Abs(angle) <= 30)
        {
            Debug.Log(angle);
            User.instance.ChangeMoney(100);

        }
        else if (Mathf.Abs(angle) > 150)
        {
            Debug.Log(angle);
            User.instance.ChangeMoney(300);
        }
        else if (angle <= -90)
        {
            Debug.Log(angle);
            User.instance.ChangeMoney(100);
        }
        else if (angle <= -30)
        {
            Debug.Log(angle);
            User.instance.ChangeMoney(600);
        }
        else if (angle <= 90)
        {
            Debug.Log(angle);
            User.instance.ChangeMoney(480);
        }
        else if (angle <= 150)
        {
            Debug.Log(angle);
            //谢谢惠顾
        }
        
        
        

    }


}
